package com.jala.user.api;

import com.jala.cm.PageResult;
import com.jala.user.model.User;
import com.jala.user.model.UserVO;
import com.jala.user.req.PageUserReq;
import com.jala.user.resp.PageUserResp;
import com.jala.user.service.UserService;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/13.
 */
@Service
public class UserApiServiceImpl implements UserApiService{
    private final Logger logger= LoggerFactory.getLogger(UserApiServiceImpl.class);

    @Autowired
    private UserService userService;

    @Override
    public PageUserResp pageUserList(PageUserReq req) {
        logger.info("pageUserList：{}", req);
        PageUserResp resp = new PageUserResp();
        PageResult<User> user = userService.pageUserList(req);
        List<User> users = user.getResults();
        List<UserVO> userVOS = new ArrayList<UserVO>();
        DozerBeanMapper mapper = new DozerBeanMapper();
        if(users!=null && users.size()>0){
            for (int i = 0; i < users.size(); i++) {
                UserVO userVO = mapper.map(users.get(i), UserVO.class);
                userVOS.add(userVO);
            }
        }
        resp.setTotalCount(user.getTotalResults());
        resp.setUserList(userVOS);
        return resp;
    }
}
